#################################################################
## Appendix Descriptive Tables
## "Criminal Victimization and Agency Attitudes in Mexico"
## Cassy Dorff
## (see 2_models_tables for model results)
#################################################################

rm(list=ls())
source(paste0("~/Dropbox/Research/solo/P2/replication/0_setup.R"))

# load the data
load(file=paste0(pathData,"1_nationalsample_data.rda"))
load(file=paste0(pathData,"2_oversample_data.rda"))

# extra libs
library(xtable)
library(knitr)
library(kableExtra)


#################################################################

#################################################################
# Subset the data to include only categorical variables
cat_ndata <- national %>%
  select(dv2, edu, community_bin, gender, javier, party_ID, region,
         civil_res_part, auto_support, urban_rural,
         victim_net_only_bin, victim_score_combined, voted)

# Calculate the counts for each category
count_table <- lapply(cat_ndata, function(x) table(x, useNA = "always"))

# Wmpty list to store the tables
var_tables <- list()

# Generate the table for each variable
for (i in seq_along(cat_ndata)) {
  variable <- names(cat_ndata)[i]
  var_table <- count_table[[i]]
  
  # Create a data frame with the variable, response, and count
  df <- data.frame(
    Variable = rep(variable, length(var_table)),
    Response = names(var_table),
    N = as.vector(var_table)
  )
  
  # Add a total row for the variable
  total_row <- data.frame(
    Variable = variable,
    Response = "Total",
    N = sum(var_table, na.rm = TRUE)
  )
  
  # Combine the data frame with the total row
  df_total <- rbind(df, total_row)
  
  # Add the variable table to the list
  var_tables[[i]] <- df_total
}

# Combine all tables into a single data frame
combined_table <- do.call(rbind, var_tables)

# Ensure consistent column structure in the combined table
combined_table <- combined_table[, c("Variable", "Response", "N")]

# Print the LaTeX table
table_latex <- kable(combined_table, format = "latex", booktabs = TRUE,
                     caption = "Summary of Categorical Variables",
                     col.names = c("Variable", "Response", "Count"),
                     linesep = "") %>%
  kable_styling(latex_options = c("hold_position"), font_size = 10)

# Add \hline between each variable group
lines <- "\\hline"
for (i in unique(combined_table$Variable)[-1]) {
  table_latex <- gsub(paste0(i, "\n"), paste0(i, "\n", lines, "\n"), table_latex)
}

# Print the LaTeX table
cat("\\begin{table}[htbp]\n")
cat(table_latex)
cat("\\end{table}\n")

#################################################################
# numeric summaries
num_tdata <- national %>% 
  select(age, lightbulbs)

summary(num_tdata)
sd(na.omit(num_tdata$age))
sd(na.omit(num_tdata$lightbulbs))




